﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using RG.Common.Provider;
using RG.Consts;
using RG.DAO;
using RealtorGuide.DAO;

namespace RealtorGuide.Classes.Impl
{
    public class MicroAreaService : IMicroAreaService
    {
        [Dependency]
        public IMicroAreaDAO MicroAreaDAO { get; set; } 


        [DAOTransaction]
        [RGTransaction]
        public MicroArea Get(int id)
        {
            return  new MicroArea().CopyFrom(MicroAreaDAO.Get(id));
        }

        [DAOTransaction]
        [RGTransaction]
        public MicroArea Put(MicroArea item)
        {
            var dao = item.Id > 0 ? MicroAreaDAO.Get(item.Id) : new MicroAreaDAOObj();

            dao = dao.CopyFrom(item);

            if (dao.Id == 0)
                MicroAreaDAO.Put(dao);

            return new MicroArea().CopyFrom(dao);
        }

        [DAOTransaction]
        [RGTransaction]
        public List<MicroArea> List(MicroAreasQueryParams param)
        {
            return MicroAreaDAO.List(param).Select(x => new MicroArea().CopyFrom(x)).ToList();
        }

        [DAOTransaction]
        [RGTransaction]
        public List<int> ListPrepare(MicroAreasQueryParams param)
        {
            return MicroAreaDAO.List(param).Select(x => x.Id).ToList();
        }
        
    }
    
    public static class IMicroAreaExtension
    {
        public static T CopyFrom<T>(this T dest,IMicroArea source)
        where T : IMicroArea
        {
            dest.Id = source.Id;
            dest.Name = source.Name;
            dest.State = source.State;
            return dest;
        }
    }
}
